/*
 * Project: Sudoku Explainer
 * Copyright (C) 2006-2007 Nicolas Juillerat
 * 
 * Project: Sudoku Hint Engine for Android
 * Copyright (C) 2010-2013 Emanuel Moecklin
 * 
 * Available under the terms of the Lesser General Public License (LGPL)
 */
package com.onegravity.sudoku.solver.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import com.onegravity.sudoku.solver.HintResource;
import com.onegravity.sudoku.solver.R;

public class HtmlLoader {

    private static final String LOAD_ERROR = "<html><body>" + "Error while loading resource: {0}</body></html>"; //$NON-NLS-1$ //$NON-NLS-2$

    private static char[] buffer = new char[4096];

    /**
     * Load the specified HTML file.
     * Custom color tags are automatically replaced by regular HTML color tags.
     * @param fileName the name of the HTML file
     * @return the content of the HTML file
     */
    public static String loadHtml(String fileName) {
		String result = null;
        try {
        	InputStream input = HintResource.getAsset(fileName);
            if (input != null) {
	            Reader reader0 = new InputStreamReader(input, "utf-8"); //$NON-NLS-1$
	            Reader reader = new BufferedReader(reader0, 4096);
	            StringBuilder builder = new StringBuilder();
	            synchronized(buffer) {
		            int read = reader.read(buffer);
		            while (read > 0) {
		                builder.append(buffer, 0, read);
		                read = reader.read(buffer);
		            }
	            }
	            reader.close();
	            result = builder.toString();
            }
            else {
                result = LOAD_ERROR.replace("{0}", fileName); //$NON-NLS-1$
            }
        }
        catch (IOException ex) {
        	Logger.e(HtmlLoader.class.getSimpleName(), ex.getMessage(), ex);
            result = LOAD_ERROR.replace("{0}", fileName); //$NON-NLS-1$
        }
        return result;
    }

    /**
     * Format a string. Replace the patterns "{0}", "{1}", etc
     * by <tt>args[0]</tt>, <tt>args[1]</tt>, etc.
     * Example: return HtmlLoader.format(result, ruleName, valueName, degreeName, firstRegion, secondRegion, Cell.toString(finCells));
     *											 {0}	   {1} 	  {2} 	 	  {3}		   {4}			 {5} 
     */
    public static String format(String source, Object... args) {
        for (int i = 0; i < args.length; i++) {
            String pattern = "{" + i + "}"; //$NON-NLS-1$ //$NON-NLS-2$
            source = source.replace(pattern, args[i].toString());
        }
        return source;
    }

    public static String formatList(String source, List<String> args) {
    	StringBuffer sb = new StringBuffer();
    	for (String arg:args) {
    		sb.append(arg);
    	}
        return source.replace("{0}", sb); //$NON-NLS-1$
    }

    public static String formatList(Object[] elements) {
        String andString = HintResource.getString(R.string.and);
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < elements.length; i++) {
            if (i > 0) result.append( (i < elements.length - 1) ? ", " : " " + andString + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            result.append(elements[i].toString());
        }
        return result.toString();
    }

    public static String formatValues(int[] values) {
        String andString = HintResource.getString(R.string.and);
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            if (i > 0)  result.append( (i < values.length - 1) ? ", " : " " + andString + " "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            result.append(Integer.toString(values[i]));
        }
        return result.toString();
    }

}